<template>
  <div class="container">
    <div class="search-box">
      <el-input placeholder="请输入内容" class="input-box" v-model="input"></el-input>
      <el-button @click="clearlist">清除</el-button>
      <el-button type="primary" @click="searchList">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="getList">
      <el-table-column type="index" label="任务编号" width="80"> </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button size="mini" style="margin-right: 10px" @click="updateList(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="Del(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        layout="prev, pager, next"
        :page-size="params._limit"
        :total="total"
        @current-change="changePage"
      >
      </el-pagination>
      <el-button size="small" plain @click="added">+ 新增</el-button>
    </div>
    <!-- 新增弹框 -->
    <el-dialog title="提示" :visible="dialogVisible" width="30%" @close="closeDialog">
      <template #default>
        <el-form ref="roleForm" label-width="100px" :model="addList" :rules="rules">
          <el-form-item label="任务名称" prop="name">
            <el-input v-model="addList.name" />
          </el-form-item>
          <el-form-item label="任务描述" prop="des">
            <el-input v-model="addList.des" />
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="confirms">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getListAPI, DelAPI, addNameAPI, updatebackAPI, updateAPI, searchAPI } from './user'
import { v4 as uuidv4 } from 'uuid'
export default {
  data() {
    return {
      getList: [],
      params: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      dialogVisible: false,
      addList: {
        name: '',
        des: '',
        id: ''
      },
      rules: {
        name: [{ required: true, message: '任务名称必填', trigger: 'blur' }],
        des: [{ required: true, message: '任务描述必填', trigger: 'blur' }]
      },
      isNew: false,
      input: '',
      // searcList: [],
      total: 0
    }
  },
  mounted() {
    this.getlist()
  },
  // 渲染列表业务
  methods: {
    async getlist() {
      const res = await getListAPI(this.params)
      this.getList = res.data
      this.total = Number(res.headers['x-total-count'])
      console.log(Number(res.headers['x-total-count']))
      // console.log(res.data)
      console.log(res)
    },
    // 分页
    changePage(page) {
      this.params._page = page
      this.getlist()
    },
    // 删除业务
    async Del(id) {
      // console.log('删除' + id)
      await DelAPI(id)
      this.getlist()
      this.$message({
        type: 'success',
        message: '删除成功!'
      })
    },
    // 新增按钮
    added() {
      this.dialogVisible = true
    },
    // 关闭前的回调
    closeDialog() {
      this.cancel()
    },
    // 取消
    cancel() {
      this.dialogVisible = false
      this.$refs.roleForm.resetFields()
      this.addList = {
        name: '',
        des: '',
        id: ''
      }
    },
    //新增业务
    confirms() {
      this.$refs.roleForm.validate(async valid => {
        if (valid) {
          if (this.addList.id) {
            this.isNew = false
            await updateAPI(this.addList)
          } else {
            this.isNew = true
            this.addList.id = uuidv4()
            const res = await addNameAPI(this.addList)
            console.log(res)
          }
          this.$message.success(`${this.isNew ? '新增' : '编辑'}成功`)
          this.getlist()
          this.cancel()
        }
      })
    },
    // 编辑回显
    async updateList(id) {
      // console.log('回显id' + id)
      this.dialogVisible = true
      const res = await updatebackAPI(id)
      console.log(res)
      this.addList = res.data
    },
    //搜索函数
    async searchList() {
      if (this.input.trim() === '') return this.$message.warning('搜索内容不可为空')
      const res = await searchAPI(this.input)
      this.getList = res.data
      this.total = res.data.length
    },
    //清除数据
    clearlist() {
      this.input = ''
      this.getlist()
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
